package com.oceansoft.mobile.econsole.modules.api.v2.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oceansoft.mobile.econsole.common.constant.ConfigInfo;
import com.oceansoft.mobile.econsole.common.constant.Constant;
import com.oceansoft.mobile.econsole.common.util.HttpClientUtil;
import com.oceansoft.mobile.econsole.common.util.web.PageUtil;
import com.oceansoft.mobile.econsole.modules.api.v2.service.ICaseService;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.jlwt.entity.SysOrg;
import com.oceansoft.mobile.econsole.modules.jlwt.persistence.ISysOrgMapper;
import com.oceansoft.mobile.econsole.modules.matters.entity.CaseInfo;
import com.oceansoft.mobile.econsole.modules.matters.entity.CaseJgEnt;
import com.oceansoft.mobile.econsole.modules.matters.entity.CaseListEnt;
import com.oceansoft.mobile.econsole.modules.matters.entity.CaseQuery;
import com.oceansoft.mobile.econsole.modules.matters.persistence.ICaseMapper;
import com.oceansoft.mobile.econsole.modules.weixin.entity.CaseLists;
import com.oceansoft.mobile.econsole.modules.weixin.entity.PoliceClass;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * Created by wzy on 2016/1/31.
 */
@Service
public class CaseServiceImpl implements ICaseService{
    @Resource
    private  ICaseMapper caseMapper;
    @Resource
    private ISysOrgMapper sysOrgMapper;

    @Resource
    protected ConfigInfo configInfo;
    @Override
    public List<SysOrg> getCaseStation(String caseGuid, String parentOrgId) {


        return caseMapper.getCaseStation(caseGuid,parentOrgId);
    }

    @Override
    public List<SysOrg> getCasePcs(String caseGuid, String parentOrgId) {
        return caseMapper.getCasePcs(caseGuid,parentOrgId);
    }

    @Override
    public List<SysOrg> getCaseRegion(String caseguid) {
        List<SysOrg> org=caseMapper.getCaseRegion(caseguid);
        for (SysOrg id : org) {
            List<SysOrg> org1=caseMapper.getCaseStation(caseguid,id.getGuid());
            id.setSysOrgList(org1);

            for (SysOrg ent :org1)
            {
                List<SysOrg> org2=caseMapper.getCasePcs(caseguid,ent.getGuid());
                ent.setSysOrgList(org2);
            }
        }

        return  org;
    }

    @Override
    public Pager<CaseListEnt> getCaseList(CaseQuery query) {
        List<SysOrg> orgs= sysOrgMapper.getOrgNs(query.getOrgId());
        query.setOrgs(orgs);

        // 分局(二级)本身挂靠的事项不需要展示，解决大厅后台部门树的BUG。
        // 三级部门本身及下属部门的事项都需要展示
        String parentGuid = sysOrgMapper.findByGuid(query.getOrgId()).getParentGuid();
        if (Objects.equals(Constant.TOP_ORG, parentGuid)) {
            query.setIsSubOrg(true);
        }

        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<CaseListEnt> page = (Page<CaseListEnt>) caseMapper.getCaseList(query);
        return PageUtil.page2pager(page, query);

    }

    @Override
    public CaseInfo getCaseInfo(String caseNo, String orgid) {
        return caseMapper.getCaseInfo(caseNo,orgid);

    }


    public List<CaseJgEnt> getJgCaseList()
    {
        return  caseMapper.getJgCaseList();
    }

    public List<SysOrg> getJgCaseOrg(String orgId,String caseno)
    {
        return  caseMapper.getJgCaseOrg(orgId,caseno);
    }



    @Override
    public void doCaseInfo(Model model , String caseNo) {
        String res = null;
        try {
            res = HttpClientUtil.doGet(configInfo.getServiceInterfaceAddress()+"/matters/"+caseNo);

//            res = HttpClientUtil.doGet("http://172.17.100.132:9008/matters/"+caseNo);
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject object = JSON.parseObject(res);
     CaseLists caseObject = JSON.parseObject(object.getString("data"), CaseLists.class);
        System.out.println(caseObject.toString());
        if(null!=caseObject.getMatProp()){
            if(caseObject.getMatProp().equals("1")){
                caseObject.setMatProp("许可");
            }else  if(caseObject.getMatProp().equals("2")){
                caseObject.setMatProp("确认");
            }else  if(caseObject.getMatProp().equals("3")){
                caseObject.setMatProp("服务");
            }else{
                caseObject.setMatProp("其他");
            }

        }
        model.addAttribute("caseInfo",caseObject);


    }

}
